package com.tutk.P2PCam264;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.VideoView;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import cn.innosmart.aq.R;

/**
 * Created by huanlinchen on 16/12/2015.
 */
public class AgzaMediaActivity extends Activity {
    private String VedioURL = "http://mediadownloads.mlb.com/mlbam/2009/05/09/mlbtv_tbabos_4494731_1m.mp4";
    private String imageFileURL = "http://androidsaveitem.appspot.com/download";
    private String imageFileURL1 = "https://tpnsuploadedfile.s3.amazonaws.com/Z7NXUWYU6GFN85PXPZGJLTFHnLwQXPJsixnJVIflwdVWMkrTXE?Signature=ndT1TWLKmkayVw0dPZBzAGnyWrM%3D&Expires=1450671714&AWSAccessKeyId=AKIAJHTAL5ZENSSP36SA";
    private ImageView mimageView;
    private VideoView mVideoView;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agza_media);
        mVideoView = (VideoView) findViewById(R.id.videoView);
        mimageView = (ImageView) findViewById(R.id.imageView);


        Bundle bundle = getIntent().getExtras();


        final String notifyInfo = bundle.getString("file");
        String mime = bundle.getString("mime");

        if(mime == null){
            startActivity(new Intent(AgzaMediaActivity.this,MainActivity.class));
            Log.d("TPNS","startact");
        }else{
                if (mime.substring(0, 5).equals("image")) {
                    progressDialog = ProgressDialog.show(AgzaMediaActivity.this,"","Loading Image wait...", true);
                    new AsyncTask<String, Void, Bitmap>() {
                        @Override
                        protected Bitmap doInBackground(String... params) {
                            return getBitmapFromURL(notifyInfo);

                        }
                        @Override
                        protected void onPostExecute(Bitmap result) {
                            progressDialog.dismiss();
                            mimageView.setImageBitmap(result);

                        }
                    }.execute(notifyInfo);

                }else{
//                    if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//                    }
                    progressDialog = ProgressDialog.show(AgzaMediaActivity.this,"","Loading Vedio wait...", true);

                    android.widget.MediaController mediaController = new android.widget.MediaController(AgzaMediaActivity.this);
                    mediaController.setAnchorView(mVideoView);
                    mVideoView.setMediaController(mediaController);
                    mVideoView.setVideoPath(notifyInfo);
                    mVideoView.requestFocus();
                    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaplayer) {
                            progressDialog.dismiss();
                            mVideoView.start();
                            Log.d("MEDIA","STARTVIDEO");
                        }
                    });
            }

        }
    }

    private static Bitmap getBitmapFromURL( String notifyInfo){

        Log.d("TPNS", "url2" + notifyInfo);
        try {
            URL url = new URL(notifyInfo);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(input);
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }



}



